package com.sdygmedia.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.domain.Category;
import com.sdygmedia.domain.CategoryChild;
import com.sdygmedia.service.ArticleService;
import com.sdygmedia.service.CategoryChildService;
import com.sdygmedia.service.CategoryService;

@Controller
public class DetailsAction extends ActionSupport {

	@Resource
	private ArticleService articleService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private CategoryChildService categoryChildService;

	private Category category;
	private CategoryChild categoryChild;
	private List articleList;

	public String article() throws Exception {
	
		List categoryList = categoryService.findAll();
		List imgList = articleService.getIndexImage();
		
		ActionContext.getContext().put("categoryList", categoryList);
		ActionContext.getContext().put("imgList", imgList);
		
		if (category != null) {
			category = categoryService.getById(category.getId());
			
			if(articleList != null){
				articleList.clear();
			}
			articleList = articleService.getByCategory(category);
			ActionContext.getContext().put("articleList", articleList);
		} else if(categoryChild != null) {
			categoryChild = categoryChildService.getById(categoryChild.getId());
			
			if(articleList != null){
				articleList.clear();
			}
			articleList = articleService.getByCategoryChild(categoryChild);
			ActionContext.getContext().put("articleList", articleList);
		}

		return "article";
	}

	public String image() throws Exception {

		List categoryList = categoryService.findAll();
		List imgList = articleService.getIndexImage();
		
		ActionContext.getContext().put("categoryList", categoryList);
		ActionContext.getContext().put("imgList", imgList);
		
		List imageList = null;

		if (category != null) {
			category = categoryService.getById(category.getId());
			imageList = articleService.getByCategory(category);
			ActionContext.getContext().put("imageList", imageList);
		} else if(categoryChild != null) {
			categoryChild = categoryChildService.getById(categoryChild.getId());
			
			imageList = articleService.getByCategoryChild(categoryChild);
			ActionContext.getContext().put("imageList", imageList);
		}

		return "image";
	}
	
	public String aboutus() throws Exception {
		List categoryList = categoryService.findAll();
		List imgList = articleService.getIndexImage();
		
		ActionContext.getContext().put("categoryList", categoryList);
		ActionContext.getContext().put("imgList", imgList);
		
		return "aboutUs";
	}

	// ----------------------------------------------------
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public CategoryChild getCategoryChild() {
		return categoryChild;
	}

	public void setCategoryChild(CategoryChild categoryChild) {
		this.categoryChild = categoryChild;
	}
}
